package api.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParamsUtils {
//    public static void addToMap(String jsonCollect, String json,Map<String, Object> map) {
//
//    }
    //id2=uid;code=code
    public static void addJsonCollectToMap(String jsonCollect, String json,Map<String, Object> map) {
        if(!JSON.isValid(json)){
            return;
        }
        Map<String, Object> stringObjectMap = StringToMapUtils.convert2(jsonCollect);
        if (MapUtils.isNotEmpty(stringObjectMap)) {
            stringObjectMap.forEach((k,v)->{
                //提取不到，提取多个
                Object jsonValue = JSONPath.read(json, String.valueOf(v));
                if(jsonValue==null){
                    //全局搜索
                    jsonValue= JSONPath.read(json, ".."+String.valueOf(v));
                    if(jsonValue!=null&&jsonValue instanceof List){
                        List<Object> list =(List) jsonValue;
                        //jmeter  name_1 name_2 name_3
                        if(!list.isEmpty()){
                            for (int i = 0; i < list.size(); i++) {
                                map.put(k+"_"+(i+1),list.get(i));
                            }
                            //${myname} 默认补了一个
                            map.put(k,list.get(0));
                        }
                    }
                }else{
                    map.put(k,jsonValue);
                }
            });
            stringObjectMap.clear();
            System.out.println("map --"+map);
        }
    }

    public static void addRegxCollectToMap(String regxCollect, String html, Map<String, Object> map) {
        Map<String, Object> stringObjectMap = StringToMapUtils.convert2(regxCollect);
        if (MapUtils.isNotEmpty(stringObjectMap)) {
            stringObjectMap.forEach((k,v)->{
                Pattern pattern=Pattern.compile(String.valueOf(v));
                Matcher matcher = pattern.matcher(html);
                int count=0; //多个自己计数
                while (matcher.find()){
                    String mapKey=matcher.group(1);
                    if(++count>=2){
                        //k_2
                        map.put(k+"_"+count,mapKey);
                    }else{
                        map.put(k,mapKey);
                    }
                }
                if(count>=2){
                   // map.put(k+"_1",map.get(0));
                    map.put(k+"_1",map.get(k));
                }
            });
            System.out.println(map);
        }

    }

    private static final String regx="\\$\\{(.*?)\\}";
    private static final Pattern pattern=Pattern.compile(regx);

    public static String replace(String str, Map<String, Object> map){
        if(StringUtils.isNotBlank(str)){
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()){
                String toRepalce = matcher.group();
                String mapKey=matcher.group(1);
                str=str.replace(toRepalce, MapUtils.getString(map,mapKey,""));
            }
        }
        return FunctionUtils.function(str);
    }
}
